package com.paas.form.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bpm.feign.model.ProcessCmd;
import com.bpm.feign.service.IBpmProcessInstanceService;
import com.paas.common.model.BaseUserInfo;
import com.paas.common.web.service.BaseServiceImpl;
import com.paas.form.dao.FormMasterApplyDao;
import com.paas.form.dto.FormMasterApplyBPMDTO;
import com.paas.form.dto.FormMasterApplyDTO;
import com.paas.form.entity.FormMasterApplyEntity;
import com.paas.form.service.IFormMasterApplyService;
import com.publics.entity.UcOrganizationEntity;
import com.publics.entity.UcUserEntity;
import com.publics.feign.service.IUcOrganizationService;
import com.publics.feign.service.IUcUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service("formMasterApplyService")
public class FormMasterApplyServiceImpl  extends BaseServiceImpl<FormMasterApplyDao,FormMasterApplyEntity> implements IFormMasterApplyService{

    @Autowired
    private IUcUserService iUcUserService;

    @Autowired
    private IUcOrganizationService iUcOrganizationService;

    //流程平台接口
    @Autowired
    private IBpmProcessInstanceService bpmProcessInstanceService;

    @Override
    public IPage<FormMasterApplyDTO> formMasterApplyList(FormMasterApplyEntity formMasterApplyEntity, Integer currentPage, Integer pageSize) {
        IPage<FormMasterApplyDTO> page = new Page<FormMasterApplyDTO>(currentPage,pageSize);
        List<FormMasterApplyDTO> formMasterApplyDTOS = baseMapper.getFormMasterApplyListDTOList(page,formMasterApplyEntity);
        setFormMasterApplyDTOSNames(formMasterApplyDTOS);
        page.setRecords(formMasterApplyDTOS);
        return page;
    }

    @Override
    public FormMasterApplyDTO formMasterApplyById(String id) {
        List<FormMasterApplyDTO> formMasterApplyDTOS = baseMapper.getFormMasterApplyListDTOById(id);
        setFormMasterApplyDTOSNames(formMasterApplyDTOS);
        return formMasterApplyDTOS.get(0);
    }

    @Override
    public void updateStatus(FormMasterApplyEntity formMasterApplyEntity, BaseUserInfo userInfo) {
        String id = formMasterApplyEntity.getId();
        String status = formMasterApplyEntity.getStatus();
        FormMasterApplyEntity formMasterApplyEntity1 = getBaseMapper().selectById(id);
        formMasterApplyEntity1.setStatus(status);
        formMasterApplyEntity1.setModifyId(userInfo.getUserId());
        formMasterApplyEntity1.setModifyDate(new Date());
        this.save(formMasterApplyEntity1);
    }

    @Override
    public void saveAndInitiate(FormMasterApplyBPMDTO<ProcessCmd> formMasterApplyBPMDTO, BaseUserInfo baseUserInfo){
        //业务参数对象
        FormMasterApplyEntity formMasterApplyEntity = formMasterApplyBPMDTO.getFormMasterApplyEntity();
        String standardSetId = formMasterApplyEntity.getId();
        //流程所需参数
        ProcessCmd processCmd = formMasterApplyBPMDTO.getProcessCmd();
        processCmd.setBusinessKey(standardSetId);
        processCmd.setData(JSON.toJSONString(formMasterApplyEntity));
        processCmd.setAppId(baseUserInfo.getAppId());
        bpmProcessInstanceService.startProcessInstance(processCmd);
        this.save(formMasterApplyEntity);
    };

    private void setFormMasterApplyDTOSNames(List<FormMasterApplyDTO> formMasterApplyDTOS) {
        if (formMasterApplyDTOS.size()>0){
            // 用户id集合
            List<String> idsList = formMasterApplyDTOS.stream().map(FormMasterApplyDTO::getApplyUserId).distinct().collect(Collectors.toList());
            // 用户信息集合
            List<UcUserEntity> userList = iUcUserService.queryByIds(idsList);
            // 用户信息集合-map
            Map<String, String> userMap = userList.stream().collect(Collectors.toMap(UcUserEntity::getId, UcUserEntity::getUserName));

            // 用户id集合
            List<String> orgIdsList = formMasterApplyDTOS.stream().map(FormMasterApplyDTO::getApplyOrgId).distinct().collect(Collectors.toList());
            // 用户信息集合
            List<UcOrganizationEntity> orgList = iUcOrganizationService.queryByIds(orgIdsList);
            // 用户信息集合-map
            Map<String, String> orgMap = orgList.stream().collect(Collectors.toMap(UcOrganizationEntity::getId, UcOrganizationEntity::getOrgName));

            // 数据填充
            for (FormMasterApplyDTO formMasterApplyDTO : formMasterApplyDTOS) {
                formMasterApplyDTO.setApplyUserName(userMap.get(formMasterApplyDTO.getApplyUserId()));
                formMasterApplyDTO.setApplyOrgName(orgMap.get(formMasterApplyDTO.getApplyOrgId()));
            }
        }
    }
}